package com.example.servicedemo;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

public class MyService extends Service {
    public MyService() {
    }
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }
    IChangeCallback mCallBack = null;
    IStudentInterface.Stub mBinder = new IStudentInterface.Stub() {
        @Override
        public int changeStudentId(String name) throws RemoteException {
            Log.i("test","changeStudentId-name="+name);
            if (name.equals( "helloworld")) {
                if (mCallBack!= null) {
                    int studentId = mCallBack.changeData(1000);
                    return studentId;
                }
                return 0;
            } else {
                return  0;
            }
        }
        @Override
        public void setCallback(IChangeCallback callback) throws RemoteException {
            mCallBack = callback;
        }
        @Override
        public String getConvertName(StudentInfo info) throws RemoteException {
            if (info!=null) {
                Log.i("test","name="+info.getName());
                info.name = "ConvertName ";
                return info.name;
            }
            return null;
        }
        @Override
        public void getServiceStudentInfo(StudentInfo serviceInfo) throws RemoteException {
            Log.i("test"," serviceInfo = " + serviceInfo);
            //serviceInfo = StudentInfo{id='null', name='null'}
            serviceInfo.id = "100";
            serviceInfo.name = "ServerChangeName";
        }
        @Override
        public void getServiceStudentInfoInOut(StudentInfo serviceInfo) throws RemoteException {
            Log.i("test","serviceInfo = " + serviceInfo);
            serviceInfo.id = "-100";
            serviceInfo.name = "clientInOut";
        }
        @Override
        public void testLinkToDeath(IChangeCallback callback) throws RemoteException {
            //linkToDeath 关联的 Binder 对象被销毁时回调binderDied方法
            callback.asBinder().linkToDeath(new DeathRecipient() {
                @Override
                public void binderDied() {
                    Log.i("test","clientDead");
                }
            },0);
        }
    };
}
